<?php
namespace app\applet\controller;

use app\applet\model\HouseKeeper;
use think\Controller;
use think\Request;
use think\Validate;

class Repair extends Controller {

    /**
     * 添加报修
     */
    public function repairAdd(){
        $model=new \app\applet\model\Repair();
        $input=input();
        if(Request::instance()->isGet()){
            $list=$model->repairList($input['id']);
            returnJson(200,"成功",$list)->send();die;
        }else{
            $this->is_prohibit($input['user_id']);
            //添加入住者
            $validate = new Validate([
                ['tel', 'require|max:11|/^1[3-8]{1}[0-9]{9}$/', '手机号码不能为空|手机号码不能超过11个字符|手机号码格式不正确'],
                ['user_id', 'require', '请登录'],
                ['house_id', 'require', '请选择住房'],
                ['type', 'require', '请选择住房'],
                ['room_number', 'require', '请选择住房'],
                ['title', 'require', '请输入报修说明'],

            ]);
            if(!$validate->check($input))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            $date=$model->repairAdd($input);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-412,"提交失败")->send();die;
            }
        }
    }
    /**
     * 报修编辑
     */
    public function repairEdit(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $this->is_prohibit($input['id']);
        if(Request::instance()->isGet()){
            $list=$model->repairOne($input);
            returnJson(200,"成功",$list)->send();die;
        }else{
            //添加入住者
            $validate = new Validate([
                ['tel', 'require|max:11|/^1[3-8]{1}[0-9]{9}$/', '手机号码不能为空|手机号码不能超过11个字符|手机号码格式不正确'],
                ['title', 'require', '请输入报修说明'],

            ]);
            if(!$validate->check($input))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            $date=$model->repairEdit($input);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-412,"提交失败")->send();die;
            }
        }
    }
    /**
     * 报修列表
     */
    public function repairList(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $this->is_prohibit($input['id']);
        $date=$model->repairIndex($input);
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 报修详情
     */
    public function repairDetails(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $this->is_prohibit($input['id']);
        $date=$model->repairDetails($input);
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 报修二次提交
     */
    public function submission(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $this->is_prohibit($input['id']);
        $date=$model->submission($input);
        returnJson(200,"成功")->send();die;
    }
    /**
     * 员工报修列表
     */
    public function staffRepair(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $date=$model->staffRepair($input);
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 员工领取任务
     */
    public function receiveTasks(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $date=$model->receiveTasks($input);
        if($date==2){
            returnJson(-410,"该报修已被领取")->send();die;
        }
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 员工报修查看
     */
    public function details(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $date=$model->details($input);
        if($date==2){
            returnJson(-410,"该报修已被领取")->send();die;
        }
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 添加维修记录
     */
    public function maintenanceRecord(){
        $model=new \app\applet\model\Repair();
        $input=input();
        //添加入住者
        $validate = new Validate([
            ['repair_id', 'require|number', '添加记录失败|添加记录失败'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }
        $date=$model->maintenanceRecord($input);
        if($date){
            returnJson(200,"成功")->send();die;
        }else{
            returnJson(-410,"添加记录失败")->send();die;
        }
    }
    /**
     * 维修记录编辑
     */
    public function recordEditor(){
        $model=new \app\applet\model\Repair();
        $input=input();
        if(Request::instance()->isGet()){
            $list=$model->recordEditorOne($input);
            if($list){
                returnJson(200,"成功",$list)->send();die;
            }else{
                returnJson(-412,"失败")->send();die;
            }

        }else{
            $list=$model->recordEditor($input);
            if($list){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-412,"失败")->send();die;
            }
        }
    }
    /**
     * 维修记录删除
     */
    public function recordDel(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $validate = new Validate([
            ['m_id', 'require|number', '删除记录失败|删除记录失败'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }
        $date=$model->recordDel($input);
        if($date){
            returnJson(200,"成功")->send();die;
        }else{
            returnJson(-410,"添加记录失败")->send();die;
        }
    }
    /**
     * 维修完成
     */
    public function maintenanceCompleted(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $validate = new Validate([
            ['repair_id', 'require|number', '提交失败|提交失败'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }
        $date=$model->maintenanceCompleted($input);
        if($date){
            returnJson(200,"成功")->send();die;
        }else{
            returnJson(-410,"添加记录失败")->send();die;
        }
    }
    /**
     * 测试
     */
    public function cd(){
        $data=$_SERVER;
        $i=$_SERVER['SERVER_NAME'];
        var_dump($data,$i);
    }

    /**
     * 判断是否被禁用
     */
    private function is_prohibit($id) {
        $model = new HouseKeeper();
        $detail = $model->detail($id);
        if ($detail['status'] == 3) {
            returnJson(-3001,"用户不存在")->send();die;
        } elseif ($detail['status'] == 2) {
            returnJson(-3000,"用户被禁用")->send();die;
        } else {

        }
    }
    /**
     * 待办事项
     */
    public function repairOrder(){
        $model=new \app\applet\model\Repair();
        $input=input();
        $list=$model->repairOrder($input['admin_id']);
        returnJson(200,"成功",$list)->send();die;
    }
}